package phapp.service.qfd.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.page.PageRequest;
import javacommon.base.*;
import phapp.model.custom.Custom;
import phapp.model.custom.CustomFavorite;
import phapp.model.qfd.*;
import phapp.dao.qfd.*;
import phapp.service.common.QfdException;
import phapp.service.custom.CustomManager;
import phapp.service.qfd.impl.*;
import phapp.service.qfd.*;
import phapp.service.system.KeywordManager;

/**
 * Title: t_custdiscuss_data 业务处理层 Description: spring事务实现层,调用Dao层取数据 Copyright:
 * Copyright (c) 2012-10 Company:
 * 
 * @author you name
 * @version 1.0
 */

@Component("custdiscussManager")
@Transactional
public class CustdiscussManagerImpl extends BaseManager<Custdiscuss, java.lang.Integer> implements CustdiscussManager
{
   public static final int FILTER_STATUS = 9;
   
   private CustdiscussDao custdiscussDao;
   private CustomManager customManager;
   private KeywordManager keywordManager;

   public void setCustdiscussDao(CustdiscussDao dao)
   {
      this.custdiscussDao = dao;
   }

   public void setCustomManager(CustomManager customManager)
   {
      this.customManager = customManager;
   }

   public void setKeywordManager(KeywordManager keywordManager)
   {
      this.keywordManager = keywordManager;
   }

   public EntityDao getEntityDao()
   {
      return this.custdiscussDao;
   }

   /**
    * <p>
    * 分页查询Custdiscuss<br>
    * 
    * @param PageRequest
    *           pr
    * @return Page
    * @author
    */
   @Transactional(readOnly = true)
   public Page findByPageRequest(PageRequest pr)
   {
      return custdiscussDao.findByPageRequest(pr);
   }

   /**
    * <p>
    * 批量删除 Custdiscuss<br>
    * 
    * @param Custdiscuss
    *           u
    * @return Boolean
    * @author
    */
   @Transactional(readOnly = true)
   public Boolean removeByIdArry(Custdiscuss u)
   {
      Boolean result = false;
      int i = -1, size = u.getIdList().size();
      i = custdiscussDao.removeByIdArry(u);
      if(i == size)
      {
         result = true;
      }
      else
      {
         throw new RuntimeException();
      }
      return result;
   }
   
   
   
   public Map customDiscuss(JSONObject jsonData)
   {
      int nStatus = 0;
      Custom customData = null;
      boolean bFilterStatus = false;
      Custdiscuss discussData = null;
      List<Custom> listQuery = null;
      String CustAccount = "", strContent = "";
      Map<String, Object> mapResult = new HashMap<String, Object>();
      Map<String, Object> mapQuery = new HashMap<String, Object>();

      try
      {
         mapQuery.clear();
         CustAccount = jsonData.getString("user");
         mapQuery.put("custAccount", CustAccount);
         listQuery = customManager.findList(mapQuery);
         if(listQuery.size() == 0 || CustAccount == null || CustAccount.length() == 0)
            throw new QfdException(2000);
         if(listQuery.size() > 1)
            throw new QfdException(2008);
         
         //判断是否包含关键词
         strContent = jsonData.getString("discusstext");
         if(keywordManager.containKeyword(strContent) == true)
            bFilterStatus = true;
         
         customData = listQuery.get(0);
         discussData = new Custdiscuss();
         discussData.setDiscussCustid(customData.getCustId());
         discussData.setCustqfdId(jsonData.getInt("custqfdid"));
         discussData.setDiscussText(jsonData.getString("discusstext"));
         if(bFilterStatus)
            discussData.setDiscussStatus(FILTER_STATUS);
         else
            discussData.setDiscussStatus(1);

         save(discussData);
         nStatus = 1000;
      }
      catch(QfdException eQfd)
      {
         nStatus = eQfd.getErrorCode();
      }
      catch(Exception eLogin)
      {
         nStatus = 10000;
         eLogin.printStackTrace();
      }
      finally
      {
         mapResult.put("statusCode", nStatus);
         if(nStatus == 1000)
            mapResult.put("message", "评论成功！");
         else if(nStatus == 2000)
            mapResult.put("message", "用户不存在！");
         else if(nStatus == 2008)
            mapResult.put("message", "存在多个用户！");
         else
            mapResult.put("message", "评论失败！");
      }
      
      return mapResult;
   }
   
   public Map getDiscussData(JSONObject jsonData)
   {
      int nStatus = 0, i = 0;
      JSONArray arrayData = null;
      JSONObject jsonDiscuss = null;
      Custdiscuss discussData = null;
      List<Custdiscuss> listData = null;
      Map<String, Object> mapResult = new HashMap<String, Object>();
      Map<String, Object> mapQuery = new HashMap<String, Object>();

      try
      {
         mapQuery.clear();
         mapQuery.put("discussStatus", 1);
         mapQuery.put("custqfdId", jsonData.getString("custqfdid"));
         listData = custdiscussDao.findList(mapQuery);
         
         arrayData = new JSONArray();
         for(i=0; i<listData.size(); i++)
         {
            discussData = listData.get(i);
            jsonDiscuss = new JSONObject();
            jsonDiscuss.put("custname", discussData.getDiscussCustName());
            jsonDiscuss.put("discusstext", discussData.getDiscussText());
            jsonDiscuss.put("discussdate", discussData.getCreateDateString());
            
            arrayData.add(jsonDiscuss);
            jsonDiscuss = null;
         }
         
         mapResult.put("discusslist", arrayData);
         nStatus = 1000;
      }
      catch(Exception eLogin)
      {
         nStatus = 10000;
         eLogin.printStackTrace();
      }
      finally
      {
         mapResult.put("statusCode", nStatus);
         if(nStatus == 1000)
            mapResult.put("message", "获取评论成功！");
         else
            mapResult.put("message", "获取评论失败！");
      }
      
      return mapResult;
   }
   
}
